'use strict';
module.exports = {
	/**
	 * 查询剧每日数据
	 * @url admin/pages/kh/tjsj/tvmysj/getList 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { type } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		let dbName = "b_copyrightData"; // 表名
		if (userInfo.role.includes("admin")) {
			res = await vk.baseDao.getTableData({
				dbName,
				data,
			});
			res.sum = res.rows.reduce((accumulator, currentValue) => accumulator + currentValue.rechargeNumber, 0);
			res.sum2 = res.rows.reduce((accumulator, currentValue) => accumulator + currentValue.commission, 0);
		}
		if (userInfo.role.includes("copyright")) {
			if(!type){
				await compute_copyright_data();
			}
			res = await vk.baseDao.getTableData({
				dbName,
				data,
				whereJson: {
					copyrightId:userInfo._id
				}
			});
			// 对 value 字段进行求和
			res.sum = res.rows.reduce((accumulator, currentValue) => accumulator + currentValue.rechargeNumber, 0);
			res.sum2 = res.rows.reduce((accumulator, currentValue) => accumulator + currentValue.commission, 0);
		}
		console.log("返回数据==》",res);
		// 业务逻辑结束-----------------------------------------------------------
		return res;
		async function compute_copyright_data (){
			let {
				todayStart,
				todayEnd
			} = vk.pubfn.getCommonTime(new Date());
			//删除实时数据
			// 返回被删除的记录条数
			await vk.baseDao.del({
			  dbName:"b_copyrightData",
			  whereJson:{
			    todayData: _.exists(true),
			  }
			});
			 let item=userInfo;
			// 业务逻辑开始-----------------------------------------------------------
			try {
				var array_copyright = item.array_copyright;
				console.log("arry_copyright", array_copyright);
				var array = [];
				for (let i of array_copyright) {
					for (let y of i.tvName) {
						array.push(y);
					}
				}
				console.log("array=》", array);
				for (let i of array) {
					//查询昨该剧充值总金额
					const rechargeNumber = await vk.baseDao.sum({
						dbName: "a_recharge",
						fieldName: "shifu_amount", // 需要求和的字段名
						whereJson: {
							tv_name: i,
							pay_status: 1,
							_add_time: _.and(_.gt(todayStart), _.lt(todayEnd)),
							isshow: true,
						}
					});
			
					if (rechargeNumber > 0) {
						let commission = 0;
						for (let y of array_copyright) {
							if (y.tvName.includes(i)) {
								console.log();
								commission = y.commission_rate * rechargeNumber * 1;
							}
						}
						console.log("");
						let id = await vk.baseDao.add({
							dbName: "b_copyrightData", // 表名
							dataJson: { // 需要新增的数据 json格式
							    "todayData":"实时数据",
								"bank": item.bank || 0,
								"paymentAccount": item.paymentAccount || 0,
								"paymentCompany": item.paymentCompany || 0,
								"copyrightId": item._id,
								"copyrightUsername": item.username,
								"copyrightNickname": item.nickname,
								"tvName": i,
								"rechargeNumber": rechargeNumber,
								"commission": commission,
								"isPayment": false,
								"_add_time": new Date().getTime(),
								"_add_time_str": vk.pubfn.timeFormat(new Date().getTime(), "yyyy-MM-dd")
							}
						});
					}
				}
				
			} catch (err) {
			
			}
			return true;
		}
	}
}
